package com.atguigu.lease.web.admin.service.impl;

import ch.qos.logback.core.joran.action.AppenderAction;
import com.atguigu.lease.common.exception.EnumException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ReleaseStatus;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {
    @Autowired
    ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    ApartmentFacilityMapper apartmentFacilityMapper;
    @Autowired
    ApartmentLabelMapper apartmentLabelMapper;
    @Autowired
    ApartmentFeeValueMapper apartmentFeeValueMapper;
    @Autowired
    GraphInfoMapper graphInfoMapper;
    @Autowired
    ProvinceInfoMapper provinceInfoMapper;
    @Autowired
    CityInfoMapper cityInfoMapper;
    @Autowired
    DistrictInfoMapper districtInfoMapper;
    @Autowired
    RoomInfoService roomInfoService;



    @Override
    public void customPage(IPage<ApartmentItemVo> iPage, ApartmentQueryVo queryVo) {
        apartmentInfoMapper.customPage(iPage, queryVo);

    }

    @Override
    public void saveByApartmentSubmitVo(ApartmentSubmitVo apartmentSubmitVo) {
        //需要保存到apartmentmapper
        Date createDate = new Date();
        //1.保存主表
        apartmentInfoMapper.saveByApartmentSubmitVo(
                /**
                 获取省份城市区域信息
                 */
                getProvinceAndCityAndDistrictInfo(apartmentSubmitVo)
        );
        System.out.println("================================");
        System.out.println(apartmentSubmitVo.getIsDeleted());
        System.out.println("================================");

        //2.保存从表
        //apartmentfacilityInfomapper，apartmentlabelmapper,apartmentfeeValuemapper,apartmentgraphmapper
        saveOtherTables(apartmentSubmitVo);
    }



    @Override
    public void updateByApartmentSubmitVo(ApartmentSubmitVo apartmentSubmitVo) {
        //1更新是主表更新，从表做删除和添加
        //1.1获取省份城市信息
         apartmentSubmitVo = getProvinceAndCityAndDistrictInfo(apartmentSubmitVo);
         //1.2跟新主表
        apartmentInfoMapper.updateById(apartmentSubmitVo);
        //34删除和添加的apartmentfacilityInfomapper，apartmentlabelmapper,apartmentfeeValuemapper,apartmentgraphmapper
        deleteOtherTablesByIds(apartmentSubmitVo.getId());
        saveOtherTables(apartmentSubmitVo);
    }

    @Override
    public ApartmentDetailVo getApartmentDetailVo(Long id) {
        return apartmentInfoMapper.getApartmentDetailVo(id);
    }
    /**
     * 查询你省份，城市，区域信息
     */
    @Override
    public ApartmentSubmitVo getProvinceAndCityAndDistrictInfo(ApartmentSubmitVo apartmentSubmitVo){
        //2.1查询省份信息
        ProvinceInfo provinceInfo = provinceInfoMapper.selectById(apartmentSubmitVo.getProvinceId());
        //2.2根据查询城市信息
        LambdaQueryWrapper<CityInfo> cityInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        cityInfoLambdaQueryWrapper.eq(CityInfo::getId,apartmentSubmitVo.getCityId());
        cityInfoLambdaQueryWrapper.eq(CityInfo::getProvinceId,apartmentSubmitVo.getProvinceId());
        CityInfo cityInfo = cityInfoMapper.selectOne(cityInfoLambdaQueryWrapper);
        //3.查询区域信息
        LambdaQueryWrapper<DistrictInfo> districtInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        districtInfoLambdaQueryWrapper.eq(DistrictInfo::getCityId,apartmentSubmitVo.getCityId());
        districtInfoLambdaQueryWrapper.eq(DistrictInfo::getId,apartmentSubmitVo.getDistrictId());
        DistrictInfo districtInfo = districtInfoMapper.selectOne(districtInfoLambdaQueryWrapper);
        apartmentSubmitVo.setProvinceName(provinceInfo.getName());
        apartmentSubmitVo.setCityName(cityInfo.getName());
        apartmentSubmitVo.setDistrictName(districtInfo.getName());
        return apartmentSubmitVo;
    }

    @Override
    public void removeByIdAndOther(Long id) {
        //-1.判断是否存在房间
        LambdaQueryWrapper<RoomInfo> roomInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomInfoLambdaQueryWrapper.eq(RoomInfo::getApartmentId,id);
        roomInfoLambdaQueryWrapper.eq(RoomInfo::getIsDeleted,0);

        long count = roomInfoService.count(roomInfoLambdaQueryWrapper);
        if (count>0){ //如果count>0，说明存在房间，不能删除
            throw new EnumException(ResultCodeEnum.DELETE_ERROR.getMessage(),ResultCodeEnum.DELETE_ERROR.getCode());
        }
        //1.删除主表
        apartmentInfoMapper.deleteById(id);
        //2.删除从表
        deleteOtherTablesByIds(id);
    }

    @Override
    public void updateStatus(Long id, ReleaseStatus status) {
        apartmentInfoMapper.updateStatusById(id,status);
    }


    /**
     * 提取保存和修改的公共部分
     * 保存从表信息
     */
    @Override
    public void saveOtherTables(ApartmentSubmitVo apartmentSubmitVo) {
        Date createDate = new Date();
        ApartmentInfo apartmentInfo = apartmentSubmitVo;
        //2.存到apartmentfacilityInfomapper
        apartmentFacilityMapper.saveByApartmentSubmitVo(apartmentInfo.getId(),apartmentSubmitVo.getFacilityInfoIds(),createDate);
        //3.存到apartmentlabelmapper
        apartmentLabelMapper.saveByApartmentSubmitVo(apartmentInfo.getId(),apartmentSubmitVo.getLabelIds(),createDate);
        //4.存到apartmentfeeValuemapper
        apartmentFeeValueMapper.saveByApartmentSubmitVo(apartmentInfo.getId(),apartmentSubmitVo.getFeeValueIds(),createDate);
        //5.存到apartmentgraphmapper
        graphInfoMapper.saveByApartmentSubmitVo(apartmentInfo.getId(),apartmentSubmitVo.getGraphVoList(),createDate);
    }

    /**
     * 删除从表的关联关系
     * @param id
     */
    @Override
    public void deleteOtherTablesByIds(Long id) {
        Date date = new Date();
        LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId,id);
        apartmentFacilityMapper.delete(apartmentFacilityLambdaQueryWrapper);

        LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId,id);
        apartmentLabelMapper.delete(apartmentLabelLambdaQueryWrapper);

        LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId,id);
        apartmentFeeValueMapper.delete(apartmentFeeValueLambdaQueryWrapper);

        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,id);
        //1代表公寓
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType,1);
        graphInfoMapper.delete(graphInfoLambdaQueryWrapper);

    }

}




